package wstrader.task;

import java.sql.*;
import pc.system.*;
import pc.sql.value.*;
import pc.util.*;
import wstrader.*;
import java.util.*;

public class Role extends Tasks {

    public static final char WN_IMPORT = 'I';
    public static final char WN_IN_PRO = 'N';
    public static final char WN_EX_PRO = 'X';
    public static final char WN_EXPORT = 'E';
    //
    public static final char WHSE_RECEIPT = 'R';
    public static final char WEIGHT_CERT = 'C';
    //
    private long focused_id = 0;
    //private int  permission = 0;

    public Role(Tasks owner, String name, String btn_name) {
        super(owner, name, btn_name);
        //this.permission = this.readPermission();
        //this.setEnable(this.permission > 0);
    }

    public Role(Tasks owner, String name) {
        this(owner, name, null);
    }

    public Role(Tasks owner) {
        super(owner);
    }

    public Users getUser() {
        return (Users) this.getRoot();
    }

    public ResultSet getMasterList(String table_name, String field_name, boolean active) {
        String sql =
                "SELECT id," + field_name + " AS name "
                + "FROM " + table_name + " "
                + "WHERE active = " + (active ? 1 : 0) + " "
                + "ORDER BY name,id";
        return this.getSQLCommand().executeQuery(sql);
    }

    public ResultSet getMasterList(String table_name, boolean active) {
        return getMasterList(table_name, "name", active);
    }

    public ResultSet getMasterList(String table_name, String field_name, String order) {
        if (field_name == null) {
            field_name = "name";
        }
        if (order == null) {
            order = "name";
        }

        String sql =
                "SELECT id," + field_name + " AS name "
                + "FROM " + table_name + " "
                + "ORDER BY " + order;
        return this.getSQLCommand().executeQuery(sql);
    }

    public ResultSet getMasterList(String table_name, String field_name) {
        return this.getMasterList(table_name, field_name, null);
    }

    public ResultSet getMasterList(String table_name) {
        return this.getMasterList(table_name, null);
    }

    public ResultSet getCompanyList(String type, boolean active) {
        String sql =
                "SELECT id,name "
                + "FROM company_master "
                + "WHERE " + type + " = 1 "
                + "  AND active = " + (active ? 1 : 0) + " "
                + "ORDER BY name,id";
        return this.getSQLCommand().executeQuery(sql);
    }

    public ResultSet getCompanyList(String type) {
        StringTokenizer st = new StringTokenizer(type, ",");
        String condition = "";
        while (st.hasMoreTokens()) {
            condition += " OR " + st.nextToken() + " = 1";
        }
        condition = condition.substring(3);
        String sql =
                "SELECT * FROM company_master "
                + "WHERE " + condition + " "
                + "ORDER BY name,id";
        return this.getSQLCommand().executeQuery(sql);
    }

    public long getLastID(String table_name) {
        return new Table(this, table_name).getLastID();
    }

    public Record getRecord(String table_name, long id) {
        return new Table(this, table_name).getRecord(id);
    }

    public ResultSet getResult(String table_name, long id) {
        return new Table(this, table_name).getResult(id);
    }
    /*
     * public long doUpdate(String table_name,long id) { Table table = new
     * Table(this,table_name); table.addParams(); return table.update(id); }
     */

    public long doDelete(String table_name, long id) {
        new Table(this, table_name).delete(id);
        return 0;
    }

    public void setFocusedID(long focused_id) {
        this.focused_id = focused_id;
    }

    public long getFocusedID() {
        return this.focused_id;
    }

    public String getNextRefNumber(String ref_number) {
        if (ref_number == null) {
            ref_number = "00-00000";
        }
        String s = ref_number.trim();
        if (s.length() < 8) {
            s = "00-00000";
        }
        int p = s.lastIndexOf(' ');

        if (p >= 0) {
            s = s.substring(p + 1).trim();
        }

        p = s.indexOf('-');
        if (p < 0) {
            p = 2;
        }
        int cy = Utils.strToInt(s.substring(0, p));
        int cn = Utils.strToInt(s.substring(p + 1));
        int dy = Utils.getCurYear() % 100;
        if (dy != cy) {
            return Utils.formatNumber(dy, 2) + "-" + Utils.formatNumber(1, 5);
        }
        return Utils.formatNumber(cy, 2) + "-" + Utils.formatNumber(cn + 1, 5);
    }

    public String getNextRefNumberForContract(String ref_number) {
        String result = "";
        String[] new_string = ref_number.split("-");
        if (new_string.length >= 3) {
            int n = Integer.parseInt(new_string[new_string.length - 1]);
            int lengthOfNum = ("" + (n + 1)).length();
            switch (lengthOfNum) {
                case 1:
                    new_string[new_string.length - 1] = "0000" + (n + 1);
                    break;
                case 2:
                    new_string[new_string.length - 1] = "000" + (n + 1);
                    break;
                case 3:
                    new_string[new_string.length - 1] = "00" + (n + 1);
                    break;
                case 4:
                    new_string[new_string.length - 1] = "0" + (n + 1);
                    break;
                case 5:
                    new_string[new_string.length - 1] = "" + (n + 1);
                    break;
                default:
                    new_string[new_string.length - 1] = "" + (n + 1);
            }
            for (int i=0; i<new_string.length; i++){
                if (result.equals("")){
                    result += new_string[i];
                }else{
                    result += "-" + new_string[i];
                }
            }
            //result = new_string[1] + "-" + new_string[2];
        }
        return result;
    }

    public String getNextRefNumber(String base, String ref_number) {
        if (base == null) {
            base = "XX 00-00000";
        }
        int p = base.lastIndexOf(' ');
        if (p >= 0) {
            base = base.substring(p + 1).trim();
        }

        if (ref_number == null) {
            ref_number = "-00";
        }
        String s = ref_number.trim();
        //if (s.length() != 14) return base + "-01";
        p = s.lastIndexOf('-');
        if (p < 0) {
            return base + "-01";
        }
        s = s.substring(p + 1).trim();
        int n = Utils.strToInt(s);
        return base + "-" + Utils.formatNumber(n + 1, 2);
    }

    public ResultSet getContract_List(char contract_type) {
        int supplier_buyer_id = getRequest().getIntValue("supplier_buyer_id");
        String sql =
                "SELECT ct.id, ct.ref_number AS name "
                + "FROM contracts ct "
                + "WHERE (ct.type = ? OR ?) "
                + "  AND (ct.supplier_buyer_id = ? OR ?) "
                + "ORDER BY name DESC";
        Vector vp = new Vector();
        vp.addElement(new StringValue("" + contract_type));
        vp.addElement(new BooleanValue(contract_type == ' '));
        vp.addElement(new IntValue(supplier_buyer_id));
        vp.addElement(new BooleanValue(supplier_buyer_id == 0));
        return getSQLCommand().executeQuery(sql, vp);
    }

    public ResultSet getBroker_List(char contract_type) {
        int supplier_buyer_id = getRequest().getIntValue("supplier_buyer_id");
        String sql =
                "SELECT ct.contract_no AS id, ct.contract_no AS name "
                + "FROM contracts ct "
                + "WHERE (ct.type = ? OR ?) "
                + "  AND (ct.supplier_buyer_id = ? OR ?) "
                + "ORDER BY name DESC";
        Vector vp = new Vector();
        vp.addElement(new StringValue("" + contract_type));
        vp.addElement(new BooleanValue(contract_type == ' '));
        vp.addElement(new IntValue(supplier_buyer_id));
        vp.addElement(new BooleanValue(supplier_buyer_id == 0));
        return getSQLCommand().executeQuery(sql, vp);
    }

    public long getContract_ID(String field) {
        long contract_id = getRequest().getLongValue("contract_id");
        String contract_no = getRequest().getStringValue(field, "0");
        if (contract_no.equals("0")) {
            return contract_id;
        }
        String sql = "SELECT * FROM contracts WHERE contract_no = '" + contract_no + "'";
        Record rc = new Record(getSQLCommand().executeQuery(sql));
        long cid = rc.getLongValue("id");
        rc.close();
        if (cid > 0) {
            contract_id = cid;
        }
        return contract_id;
    }

    public long getContract_ID() {
        return getContract_ID("contract_no");
    }
}
